﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


//-------------------------------PUZ-------------------------------//



public delegate void FunBack();//无返回值无参的委托类型 
public class UITween : MonoBehaviour
{
    private Vector2 startPos;//初始位置
    private Vector2 targetPos;//目标位置
    public Vector2 offsetPos;//偏移量

    FunBack funBack;//委托变量
    RectTransform rect;

    // Start is called before the first frame update
    void Awake()
    {
        startPos = transform.position;//当前位置
        rect = GetComponent<RectTransform>();
    }

    public IEnumerator MoveTo()
    {
        Vector2 target2 = targetPos;
        while (Mathf.Abs(rect.position.x - target2.x) > 10f)
        {
            rect.position = Vector2.Lerp(rect.position, target2, 0.2f);
            yield return new WaitForFixedUpdate();//固定每帧等待更新
        }        
        if (funBack != null)
        {
            funBack();//回调函数
            funBack = null;
        }
        StopIE();
    }

    public void SetButtonHandle(FunBack fun)
    {
        funBack = fun;
    }

    //停止协程
    public void StopIE()
    {
        StopCoroutine(MoveTo());
    }


    public void SetStartPos()//起始位置
    {
        targetPos = startPos;
    }
    public void SetTargetPos()//目标位置
    {
        targetPos = offsetPos + new Vector2(Screen.width / 2, Screen.height / 2);//坐标转换（屏幕中心为原点）
    }
}
